package 多线程.线程t2优先于t1交替打印;

/**
 * 如果想保证t2在t1之前打印,保证首先输出的是A而不是1,这个时候该如何做？
 * 设置一个boolean类型的变量,t2刚开始没执行
 * t1线程就wait,要求t2必须先执行才能执行后面的业务逻辑
 */
public class sync_wait_notify {
    private static volatile boolean t2Started = false;
    public static void main(String[] args) {
        final Object o = new Object();
        char[] arr1 = "1234567".toCharArray();
        char[] arr2 = "ABCDEFG".toCharArray();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                synchronized (o) {
                    while (!t2Started) {
                        try {
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    for (char c : arr1) {
                        System.out.print(c);
                        try {
                            o.notify();
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                synchronized (o) {
                    for (char c : arr2) {
                        System.out.print(c);
                        t2Started = true;
                        try {
                            o.notify();
                            o.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    o.notify();
                }
            }
        };
        t1.start();
        t2.start();
    }


    /**
     * lambada表达式写法
     */
    public static void main2(String[] args) {
        final Object o = new Object();
        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();
        new Thread(() -> {
            synchronized (o) {
                while (!t2Started) {
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                for (char c : aI) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t1").start();
        new Thread(() -> {
            synchronized (o) {
                for (char c : aC) {
                    System.out.print(c);
                    t2Started = true;
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                o.notify();
            }
        }, "t2").start();
    }
}
